﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.RoleFunctionServiceRef;

    public class Role : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreated;
        protected HtmlForm F1011;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hidButtonFalse;
        protected HtmlInputHidden hidButtonTrue;
        protected HtmlInputHidden hidFunctionFalse;
        protected HtmlInputHidden hidFunctionTrue;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblRoleId;
        protected Label lblRoleName;
        protected HtmlLink mycss;
        protected TextBox txtRoleId;
        protected TextBox txtRoleName;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtRoleId.Text.Trim()))
            {
                try
                {
                    List<string> gInitLocal0 = new List<string> {
                        this.txtRoleId.Text
                    };
                    List<string> roleId = gInitLocal0;
                    this.lblMsg.Visible = true;
                    this.lblMsg.Text = RoleFunctionServiceClientProxy.GetInstance().DeleteRoleAndRoleFunction(roleId) + base.GetGlobalMsgResource("F1100_009");
                    this.txtRoleId.Text = "";
                    this.txtRoleName.Text = "";
                    this.hdnFlag.Value = "0";
                    this.txtRoleId.Enabled = true;
                }
                catch
                {
                }
            }
            else
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_004");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("RoleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("RoleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("Role.aspx?from=" + this.hdnFrom.Value);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtRoleId.Text.Trim()))
            {
                int i;
                bool Type;
                int j;
                string FunctionIDFalse = this.hidFunctionFalse.Value;
                string FunctionIDTrue = this.hidFunctionTrue.Value;
                string OperationIDFalse = this.hidButtonFalse.Value;
                string OperationIDTrue = this.hidButtonTrue.Value;
                string roleID = this.txtRoleId.Text.Trim();
                List<string> FunctionGidTrueList = new List<string>();
                if (!string.IsNullOrEmpty(FunctionIDTrue))
                {
                    string[] functionGidT = FunctionIDTrue.Substring(0, FunctionIDTrue.Length - 1).Split(new char[] { ',' });
                    for (i = 0; functionGidT.Length > i; i++)
                    {
                        if (FunctionGidTrueList.Count > 0)
                        {
                            Type = false;
                            j = 0;
                            while (FunctionGidTrueList.Count > j)
                            {
                                if (FunctionGidTrueList[j] == functionGidT[i])
                                {
                                    Type = false;
                                    break;
                                }
                                Type = true;
                                j++;
                            }
                            if (Type)
                            {
                                FunctionGidTrueList.Add(functionGidT[i]);
                            }
                        }
                        else
                        {
                            FunctionGidTrueList.Add(functionGidT[i]);
                        }
                    }
                }
                List<string> FunctionIDFalseList = new List<string>();
                if (!string.IsNullOrEmpty(FunctionIDFalse))
                {
                    string[] functionGidF = FunctionIDFalse.Substring(0, FunctionIDFalse.Length - 1).Split(new char[] { ',' });
                    for (i = 0; functionGidF.Length > i; i++)
                    {
                        FunctionIDFalseList.Add(functionGidF[i]);
                    }
                }
                List<string> OperationGIDTrueList = new List<string>();
                if (!string.IsNullOrEmpty(OperationIDTrue))
                {
                    string[] operationGidT = OperationIDTrue.Substring(0, OperationIDTrue.Length - 1).Split(new char[] { ',' });
                    for (i = 0; operationGidT.Length > i; i++)
                    {
                        if (OperationGIDTrueList.Count > 0)
                        {
                            Type = false;
                            for (j = 0; OperationGIDTrueList.Count > j; j++)
                            {
                                if (OperationGIDTrueList[j] == operationGidT[i])
                                {
                                    Type = false;
                                    break;
                                }
                                Type = true;
                            }
                            if (Type)
                            {
                                OperationGIDTrueList.Add(operationGidT[i]);
                            }
                        }
                        else
                        {
                            OperationGIDTrueList.Add(operationGidT[i]);
                        }
                    }
                }
                List<string> OperationGIDFalseList = new List<string>();
                if (!string.IsNullOrEmpty(OperationIDFalse))
                {
                    string[] operationGidF = OperationIDFalse.Substring(0, OperationIDFalse.Length - 1).Split(new char[] { ',' });
                    for (i = 0; operationGidF.Length > i; i++)
                    {
                        OperationGIDFalseList.Add(operationGidF[i]);
                    }
                }
                try
                {
                    string strmsg = string.Empty;
                    WMS6.Web.RoleFunctionServiceRef.RoleInfo roleInfo = new WMS6.Web.RoleFunctionServiceRef.RoleInfo {
                        RoleID = this.txtRoleId.Text,
                        RoleName = this.txtRoleName.Text
                    };
                    if (string.IsNullOrEmpty(this.hdnCopy.Value))
                    {
                        roleInfo.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                    }
                    else
                    {
                        this.chkSysCreated.Checked = false;
                        roleInfo.SystemCreated = "N";
                    }
                    roleInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    roleInfo.CreatedBy = base.UserInfo.UserID;
                    this.Session.Remove("RoleFunctionData");
                    this.lblMsg.Visible = true;
                    strmsg = RoleFunctionServiceClientProxy.GetInstance().UpdateRoleFunctionAndOperationByRoleGID(this.hdnFlag.Value, roleInfo, FunctionGidTrueList, FunctionIDFalseList, OperationGIDTrueList, OperationGIDFalseList, this.hdnCopy.Value);
                    this.hidFunctionFalse.Value = "";
                    this.hidFunctionTrue.Value = "";
                    this.hidButtonTrue.Value = "";
                    this.hidButtonFalse.Value = "";
                    this.lblMsg.Visible = true;
                    this.hdnFlag.Value = "1";
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1100_001") + strmsg;
                    this.txtRoleId.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1100_002") + ex.Message;
                    this.txtRoleId.Text = "";
                }
            }
            else
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = base.GetGlobalMsgResource("F1100_003");
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.SetRoleInfo2Form();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Buffer = true;
            base.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1.0);
            base.Response.Expires = 0;
            base.Response.CacheControl = "no-cache";
            base.Response.AddHeader("Pragma", "No-Cache");
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetRoleInfo2Form()
        {
            string roleId = base.Request.QueryString["ID"];
            if (!string.IsNullOrEmpty(roleId))
            {
                this.hdnFlag.Value = "1";
                RoleQueryEntity roleQuery = new RoleQueryEntity();
                WMS6.Web.BaseInfoServiceRef.RoleInfo role = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(roleId, "ROLE") as WMS6.Web.BaseInfoServiceRef.RoleInfo;
                if (role != null)
                {
                    this.txtRoleId.Text = role.RoleID;
                    this.txtRoleName.Text = role.RoleName;
                    this.chkIsActive.Checked = role.IsActive == "Y";
                    this.chkSysCreated.Checked = role.SystemCreated == "Y";
                    this.txtRoleId.Enabled = false;
                }
            }
            else
            {
                this.hdnFlag.Value = "0";
            }
        }
    }
}

